/************************************************************************************/
/*  Program name CAPFNDIMP_LOCATION.sql                                             */
/*  Creater:  WONG Wing Kin (PCCW - UniHub)                                         */
/*************   History   *************************                                */
/* 30 NOV, 2005   WONG Wing Kin         First created                               */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

--ALTER TRIGGER DR$HR_LOCATIONS_N1TC DISABLE;
ALTER TRIGGER HR_LOCATIONS_ALL_OVN DISABLE;
ALTER TRIGGER HR_LOCATIONS_ALL_WHO DISABLE;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPFNDIMP_LOCATION';

	lv_Scc_Count	NUMBER			:=	0;
	lv_Err_Count	NUMBER			:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_FND_LOCATION;

	lv_Next_Seq_ID	NUMBER;

BEGIN

	--  RENAME
	UPDATE
		HR_LOCATIONS_ALL
	SET
		Location_Code	=	NVL(CAPBRIMP_MAPPING.RenameStr(SUBSTR(Location_Code,5,4),Location_Code),
								CAPBRIMP_MAPPING.RenameStr(SUBSTR(Location_Code,1,4),Location_Code))
		--Description		=	CAPBRIMP_MAPPING.RenameStr(SUBSTR(Location_Code,4,4),Description)		
	WHERE	Location_ID	IN	(
									SELECT
										Location_ID
									FROM
										CRST_FND_LOCATION
									);

	FOR C1 IN Cur_F LOOP
		BEGIN
			SELECT	HR_LOCATIONS_S.NextVal
			INTO	lv_Next_Seq_ID
			FROM	DUAL;

			INSERT INTO CRST_FND_LOCATION_MAP VALUES
			(
			C1.LOCATION_ID,
			CAPBRIMP_MAPPING.RenameStr(SUBSTR(C1.Location_Code,4,4),C1.Location_Code),
			C1.BUSINESS_GROUP_ID,
			lv_Next_Seq_ID,
			C1.Location_Code,
			CAPBRIMP_COMMON.GetBGID(C1.BUSINESS_GROUP_NAME)
			);

			INSERT INTO HR_LOCATIONS_ALL
			VALUES
			(
			lv_Next_Seq_ID,											--LOCATION_ID,
			C1.LOCATION_CODE,
			C1.BUSINESS_GROUP_ID,		--BUSINESS_GROUP_ID,
			C1.DESCRIPTION,
			C1.SHIP_TO_LOCATION_ID,									--Update Below
			C1.SHIP_TO_SITE_FLAG,
			C1.RECEIVING_SITE_FLAG,
			C1.BILL_TO_SITE_FLAG,
			C1.IN_ORGANIZATION_FLAG,
			C1.OFFICE_SITE_FLAG,
			C1.DESIGNATED_RECEIVER_ID,
			CAPBRIMP_COMMON.GetOrgID(C1.INVENTORY_ORG_NAME),		--INVENTORY_ORGANIZATION_ID,
			C1.TAX_NAME,
			C1.INACTIVE_DATE,
			C1.STYLE,
			C1.ADDRESS_LINE_1,
			C1.ADDRESS_LINE_2,
			C1.ADDRESS_LINE_3,
			C1.TOWN_OR_CITY,
			C1.COUNTRY,
			C1.POSTAL_CODE,
			C1.REGION_1,
			C1.REGION_2,
			C1.REGION_3,
			C1.TELEPHONE_NUMBER_1,
			C1.TELEPHONE_NUMBER_2,
			C1.TELEPHONE_NUMBER_3,
			C1.LOC_INFORMATION13,
			C1.LOC_INFORMATION14,
			C1.LOC_INFORMATION15,
			C1.LOC_INFORMATION16,
			C1.LOC_INFORMATION17,
			C1.ATTRIBUTE_CATEGORY,
			C1.ATTRIBUTE1,
			C1.ATTRIBUTE2,
			C1.ATTRIBUTE3,
			C1.ATTRIBUTE4,
			C1.ATTRIBUTE5,
			C1.ATTRIBUTE6,
			C1.ATTRIBUTE7,
			C1.ATTRIBUTE8,
			C1.ATTRIBUTE9,
			C1.ATTRIBUTE10,
			C1.ATTRIBUTE11,
			C1.ATTRIBUTE12,
			C1.ATTRIBUTE13,
			C1.ATTRIBUTE14,
			C1.ATTRIBUTE15,
			C1.ATTRIBUTE16,
			C1.ATTRIBUTE17,
			C1.ATTRIBUTE18,
			C1.ATTRIBUTE19,
			C1.ATTRIBUTE20,
			C1.GLOBAL_ATTRIBUTE_CATEGORY,
			C1.GLOBAL_ATTRIBUTE1,
			C1.GLOBAL_ATTRIBUTE2,
			C1.GLOBAL_ATTRIBUTE3,
			C1.GLOBAL_ATTRIBUTE4,
			C1.GLOBAL_ATTRIBUTE5,
			C1.GLOBAL_ATTRIBUTE6,
			C1.GLOBAL_ATTRIBUTE7,
			C1.GLOBAL_ATTRIBUTE8,
			C1.GLOBAL_ATTRIBUTE9,
			C1.GLOBAL_ATTRIBUTE10,
			C1.GLOBAL_ATTRIBUTE11,
			C1.GLOBAL_ATTRIBUTE12,
			C1.GLOBAL_ATTRIBUTE13,
			C1.GLOBAL_ATTRIBUTE14,
			C1.GLOBAL_ATTRIBUTE15,
			C1.GLOBAL_ATTRIBUTE16,
			C1.GLOBAL_ATTRIBUTE17,
			C1.GLOBAL_ATTRIBUTE18,
			C1.GLOBAL_ATTRIBUTE19,
			C1.GLOBAL_ATTRIBUTE20,
			C1.LAST_UPDATE_DATE,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_BY_NAME),		--LAST_UPDATED_BY,
			C1.LAST_UPDATE_LOGIN,
			CAPBRIMP_COMMON.GetUser(C1.CREATED_BY_NAME),			--CREATED_BY,
			C1.CREATION_DATE,
			CAPBRIMP_COMMON.GetUser(C1.ENTERED_BY_NAME),			--ENTERED_BY,
			C1.TP_HEADER_ID,
			C1.ECE_TP_LOCATION_CODE,
			C1.OBJECT_VERSION_NUMBER,
			NULL,	--GEOMETRY,
			C1.LOC_INFORMATION18,
			C1.LOC_INFORMATION19,
			C1.LOC_INFORMATION20,
			C1.DERIVED_LOCALE
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;

	BEGIN
		UPDATE	HR_LOCATIONS_ALL
		SET
			SHIP_TO_LOCATION_ID	=	CAPBRIMP_MAPPING.GetLocationIDMapping(SHIP_TO_LOCATION_ID)
		WHERE	LOCATION_ID	IN	(
								SELECT
									LOCATION_ID_N
								FROM
									CRST_FND_LOCATION_MAP
								);
	EXCEPTION
		WHEN OTHERS THEN
			DBMS_OUTPUT.Put_Line(SQLERRM);
			lv_Err_Count	:=	lv_Err_Count + 1;
	END;

	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/

--ALTER TRIGGER DR$HR_LOCATIONS_N1TC ENABLE;
ALTER TRIGGER HR_LOCATIONS_ALL_OVN ENABLE;
ALTER TRIGGER HR_LOCATIONS_ALL_WHO ENABLE;
